<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Menu_model extends CI_Model {

	public $menu_tbl 		= 'menus';
	public $rolemenu_tbl 	= 'role_menus';
	public $role_tbl		= 'roles';
	
	public function __construct()
    {
        parent::__construct();
    }
    
    // ------------------------------------------------------------------------
	
	public function list_menus($limit = '', $offset = '', $order = 'name', $where = '')
	{
		$offset = $offset > 0 ? ($offset - 1) * $limit : 0;
		
		if ($where) $this->db->where($where);
		
		$query = $this->db->select()
						  ->where('deleted', 'n')
						  ->order_by($order)
						  ->get($this->menu_tbl, $limit, $offset);
						  
		return $query->num_rows() > 0 ? $query->result_array() : FALSE;
	}
	
	// ------------------------------------------------------------------------
	
	public function create_menu($fields)
	{
		$this->db->insert($this->menu_tbl, $fields);
		
		return $this->db->affected_rows() > 0 ? $this->get_menu($this->db->insert_id()) : FALSE;
	}
	
	// ------------------------------------------------------------------------
	
	public function get_role_menus($role_id)
	{
		$query = $this->db->select('r.*, GROUP_CONCAT(rm.menu_id) AS menus')
						  ->from($this->role_tbl . ' AS r')
						  ->join($this->rolemenu_tbl . ' AS rm', 'rm.role_id = r.role_id', 'left')
						  ->where('r.role_id', $role_id)
						  ->get();
						  
		return $query->num_rows() > 0 ? $query->row_array() : FALSE;
	}
	
	// ------------------------------------------------------------------------
	
	public function update_role_menus($fields, $role_id)
	{
		$this->delete_role_menus($role_id);
		$this->create_role_menus($role_id, $fields['menus']);
		
		return TRUE;
	}
	
	// ------------------------------------------------------------------------
	
	public function delete_role_menus($value, $where = 'role_id')
	{
		$query = $this->db->where($where, $value)
						  ->delete($this->rolemenu_tbl);
						  
		return TRUE;
	}
	
	// ------------------------------------------------------------------------
	
	public function create_role_menus($role_id, $fields)
	{
		foreach ($fields as $key => $menu)
		{
			$data[] = array(
				'menu_id'	=> $menu,
				'role_id'	=> $role_id,
				'position'	=> $key + 1
			);
		}
		
		return $this->db->insert_batch($this->rolemenu_tbl, $data);
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * List Role Menus
	 *
	 * List all menus based with position using the given role id,
	 * Needed for Edit Role as the position of the menu is important
	 *
	 * @param	int		role_id
	 * @return	array|bool
	 */
	public function list_role_menus($role_id)
	{
		$query = $this->db->select('m.*, IF(rm.position IS NULL, 99, rm.position) as position', FALSE)
						  ->from($this->menu_tbl . ' AS m')
						  ->join($this->rolemenu_tbl . ' AS rm', 'rm.menu_id = m.menu_id AND rm.role_id = ' . $role_id, 'left')
						  ->where('m.deleted', 'n')
						  ->order_by('position, name')
						  ->get();
						  
		return $query->num_rows() > 0 ? $query->result_array() : FALSE;
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * List Menu Role
	 *
	 * List all menus assigned to a role
	 *
	 * @param	int
	 * @return 	array|bool
	 */
	public function list_menu_role($role_id)
	{
		$query = $this->db->select('m.*')
						  ->from($this->rolemenu_tbl . ' AS rm')
						  ->join($this->menu_tbl . ' AS m', 'm.menu_id = rm.menu_id AND m.deleted = \'n\'', 'left')
						  ->where('rm.role_id', $role_id)
						  ->order_by('rm.position, name')
						  ->get();
						  
		return $query->num_rows() > 0 ? $query->result_array() : FALSE;
	}
	
	// ------------------------------------------------------------------------
	
	public function update_menu($fields, $menu_id)
	{
		$query = $this->db->where('menu_id', $menu_id)
						  ->where('deleted', 'n')
						  ->update($this->menu_tbl, $fields);
						  
		return TRUE;
	}
	
	// ------------------------------------------------------------------------
	
	public function get_menu($menu_id)
	{
		$query = $this->db->where('menu_id', $menu_id)
						  ->where('deleted', 'n')
						  ->get($this->menu_tbl);
						  
		return $query->num_rows() > 0 ? $query->row_array() : FALSE;
	}
	
	// ------------------------------------------------------------------------
	
	public function delete_menu($menu_id)
	{
		$menu = $this->get_menu($menu_id);
		
		if ($menu['parent_menu'] == 0)
		{
			$query = $this->db->where('parent_menu', $menu_id)
							  ->where('deleted', 'n')
							  ->update($this->menu_tbl, array('deleted' => 'y'));
		}
		
		$query = $this->db->where('menu_id', $menu_id)
						  ->where('deleted', 'n')
						  ->update($this->menu_tbl, array('deleted' => 'y'));
						  
		$this->delete_role_menus($menu_id, 'menu_id');
		
		return TRUE;
	}
	
	// ------------------------------------------------------------------------
	
	public function count_menus()
	{
		$query = $this->db->select('menu_id')
						  ->where('deleted', 'n')
						  ->get($this->menu_tbl);
						  
		return $query->num_rows();
	}
	
	// ------------------------------------------------------------------------
	
	
}

/* End of file menu_model.php */
/* Location: ./application/models/menu_model.php */